/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package VocabularyManagement;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;

import GuiTools.*;
import Mainframe.Mainframe;
import Settings.*;
import Storage.Storage;

/**
 * In this dialog the settings for importing a vocabulary in another format than Tribuit's own format are made.  
 * 
 * @author Christoph Schnack
 *
 */
public class ImportVocabulary {
	Settings SET;
    Language LANG;
    Storage MAINVOC;
    Mainframe MAINFRAME;
    
    TribuitTextField langATF,langBTF;
    JLabel fileLabel;
    String pathToImportFile;
    
    JDialog ivFrame;
    
	public ImportVocabulary(Settings set, Storage str, Mainframe mf){
		MAINVOC=str;
    	SET=set;
    	LANG=SET.getLanguage();
    	MAINFRAME=mf;
		buildGUI();
	}
	
	public void buildGUI(){
		ivFrame = new JDialog(MAINFRAME, true);
		ivFrame.setTitle(LANG.getImportVocabulary());
		
		JPanel centerPanel = new JPanel();
		
		JPanel filePanel = new JPanel();
		filePanel.setBorder(BorderFactory.createTitledBorder(LANG.getFileToImport()+":"));
		fileLabel = new JLabel("-");
		JButton selectFileBtn = new JButton(LANG.getSelectFile());
		selectFileBtn.addActionListener(new SelectFileBtnListener());
		filePanel.add(selectFileBtn);
		filePanel.add(fileLabel);
		
		langATF = new TribuitTextField(SET, 10);
		langBTF = new TribuitTextField(SET, 10);
		JPanel namePanel = new JPanel();
		namePanel.setBorder(BorderFactory.createTitledBorder(LANG.getNewVocabularyName()));
		namePanel.add(new LabelPanel(LANG.getLanguage()+" 1",langATF));
		namePanel.add(new LabelPanel(LANG.getLanguage()+" 2",langBTF));
		
		centerPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.weighty = 1;
		centerPanel.add(filePanel,gbc);
		gbc.gridy = 1;
		centerPanel.add(namePanel,gbc);
		
		JButton startImportBtn = new JButton(LANG.getImport());
		startImportBtn.addActionListener(new StartImportBtnListener());
		JButton cancelBtn = new JButton(LANG.getCancel());
		cancelBtn.addActionListener(new CancelBtnListener());
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(GuiTools.BGCOLOR);
		buttonPanel.add(startImportBtn);
		buttonPanel.add(cancelBtn);
		
		ivFrame.getContentPane().add(BorderLayout.NORTH,new Header(LANG.getImportVocabulary()));
		ivFrame.getContentPane().add(BorderLayout.CENTER, centerPanel);
		ivFrame.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
		ivFrame.setLocation(100,100);
		ivFrame.pack();
		ivFrame.setVisible(true);
	}
	
	private class SelectFileBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			JFileChooser fileDialog = new JFileChooser();
			fileDialog.setDialogTitle(LANG.getFileToImport());
			fileDialog.showDialog(ivFrame,LANG.getSelect());
			File f = fileDialog.getSelectedFile();
			if(f!=null){
				pathToImportFile = f.getAbsolutePath();
				fileLabel.setText(f.getName());
				ivFrame.pack();
			}
		}
	}
	
	private class StartImportBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(pathToImportFile==null){
				JOptionPane.showMessageDialog(ivFrame, LANG.getChooseFile(), LANG.getAttention(), JOptionPane.ERROR_MESSAGE);
				return;
			}
			File impFile = new File(pathToImportFile);
			if(!impFile.exists()){
				JOptionPane.showMessageDialog(ivFrame, LANG.getFileDoesntExistAnyMore(), LANG.getAttention(), JOptionPane.ERROR_MESSAGE);
				return;
			}
			String nameA = langATF.getText(),nameB = langBTF.getText();
			if(nameA == null || nameA.trim().equals("")){
				GuiTools.showEmptyOrInvalidInputWarning(LANG, ivFrame, "("+LANG.getLanguage()+" 1)");
				langATF.grabFocus();
				return;
			}
			if(!GuiTools.checkInput(nameA.trim())){
				GuiTools.showEmptyOrInvalidInputWarning(LANG, ivFrame, "("+LANG.getLanguage()+" 1)");
				langATF.grabFocus();
				return;
			}
			if(nameB == null || nameB.trim().equals("")){
				GuiTools.showEmptyOrInvalidInputWarning(LANG, ivFrame, "("+LANG.getLanguage()+" 2)");
				langBTF.grabFocus();
				return;
			}
			if(!GuiTools.checkInput(nameB.trim())){
				GuiTools.showEmptyOrInvalidInputWarning(LANG, ivFrame, "("+LANG.getLanguage()+" 2)");
				langBTF.grabFocus();
				return;
			}
			String newName = nameA.trim() + " - " + nameB.trim();
			if(MAINVOC.hasVocabulary(newName)){
				JOptionPane.showMessageDialog(ivFrame, LANG.getVocabularyAlreadyExists(), LANG.getAttention(), JOptionPane.ERROR_MESSAGE);
				return;
			}
			System.out.println(pathToImportFile);
		}
	}
	
	private class CancelBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			ivFrame.dispose();
		}
	}

//	private class SeparatorPanel extends JPanel{
//		String separator;
//		JLabel sepDisplay;
//		public SeparatorPanel(){
//			super();
//			sepDisplay = new JLabel();
//			JButton btn = new JButton(((Object) LANG).getSetSeparator());
//			btn.addActionListener(new SetSepListener());
//			setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
//			setBorder(BorderFactory.createLineBorder(Color.black));
//			setBackground(Color.blue);
//		}
//		
//		private class SetSepListener implements ActionListener{
//			public void actionPerformed(ActionEvent e){
//				String sep = JOptionPane.showInputDialog((LANG).getInsertSeparatorString());
//				if(sep==null){
//				//	JOptionPane.showMessageDialog(ivFrame, ((Object) LANG).getWarningNoSeparatorString(), LANG.getAttention(), JOptionPane.WARNING_MESSAGE);
//				}
//				else{
//					sep = sep.trim();
//					sepDisplay.setText(sep);
//					separator = sep;
//				}
//			}
//		}
//	}
}
